package com.oa.service.impl;

import com.oa.bean.Customer;
import com.oa.bean.Emp;
import com.oa.dao.CustomerDao;
import com.oa.dao.EmpDao;
import com.oa.service.CustomerService;
import com.oa.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private EmpDao empDao;

    @Override
    public PageBean pageQuery(PageBean pageBean) {
        return customerDao.pagingQuery(pageBean);
    }

    @Override
    public void add(Customer model) {

        customerDao.save(model);

        Emp emp = empDao.findById(model.getEmp().getId());

        emp.getCustomers().add(model);

        model.setEmp(emp);
    }

    @Override
    public void delete(Integer id) {

        Customer cus = customerDao.findById(id);

        Emp emp = cus.getEmp();

        emp.getCustomers().remove(cus);

        cus.setIs(0);

        customerDao.update(cus);
    }

    @Override
    public Customer beforeEdit(Integer id) {

        return customerDao.findById(id);
    }

    @Override
    public void update(Customer model) {

        customerDao.update(model);

        Emp emp = empDao.findById(model.getEmp().getId());

        emp.getCustomers().add(model);

        model.setEmp(emp);
        
    }
}
